﻿using MQXT.Repository.DAL;
using MQXT.Repository.Model;
using Oliving.CommonPC.Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MQXT.BLL
{
    public static class EdgeDataAnalysis
    {



        /// <summary>
        /// 验证是否有注册信息
        /// </summary>
        /// <param name="dataContent"></param>
        /// <returns></returns>
        public static bool Check_ZhuCe(string dataContent, int sessionID)
        {

            bool isBegin = (dataContent.Substring(0, 5) == "addr:");

            int isEnd = dataContent.IndexOf(";over;");

            if (isBegin && (isEnd > 0))
            {
                dataContent = dataContent.Substring(0, isEnd);


                if (dataContent.Contains("sim:")
                    && dataContent.Contains("yb:")
                    && dataContent.Contains("gps:")
                    && dataContent.Contains("ver:")
                    && dataContent.Contains("counter:")
                    && dataContent.Contains("reg1:")
                    && dataContent.Contains("reg2:")
                    && dataContent.Contains("reg3:")
                    && dataContent.Contains("reg4:")
                    && dataContent.Contains("reg5:")
                    && dataContent.Contains("reg6:")
                    && dataContent.Contains("reg7:")
                    && dataContent.Contains("reg8:")
                    && dataContent.Contains("reg9:")
                    && dataContent.Contains("reg10:")
                    )
                {
                    // 注册信息
                    CaiJiQiModel cjq = Get_CaiJiQiModel(dataContent);

                    CaiJiQiModelService bll = new CaiJiQiModelService();
                    cjq.SesssionID = sessionID.ToString();


                    //EdgeLog.writeLog(
                    //    "CJQ_注册",
                    //    Newtonsoft.Json.JsonConvert.SerializeObject(cjq));    // 字符串


                    bll.AddOrUpdate(cjq);

                }
                else
                {
                    // 上报信息
                    CaiJiQiModelValue cjqV = Get_CaiJiQiModelValue(dataContent);

                    CaiJiQiModelService bllCJQ = new CaiJiQiModelService();
                    CaiJiQiModelValueService bll = new CaiJiQiModelValueService();
                    bll.Add(cjqV);

                    CaiJiQiModel dto = bllCJQ.Query("   CJQ_Addr='" + cjqV.CJQ_Addr + "'").FirstOrDefault();
                    //EdgeLog.writeLog(
                    //      "CJQ_上报1",
                    //      Newtonsoft.Json.JsonConvert.SerializeObject(dto));    // 字符串


                    if (dto != null && dto.CJQ_Id > 0)
                    {

                        //EdgeLog.writeLog(
                        //    "CJQ_上报",
                        //    Newtonsoft.Json.JsonConvert.SerializeObject(dto));    // 字符串


                        // 从站地址   dto.CJQ_Yb
                        // 寄存器     dto.CJQ_JCQ1
                        // 寄存器值   cjqV.CJQ_JCQ1 
                        DataShowModelService bll_DataShowModelService = new DataShowModelService();

                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ1, cjqV.CJQ_JCQ1);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ2, cjqV.CJQ_JCQ2);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ3, cjqV.CJQ_JCQ3);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ4, cjqV.CJQ_JCQ4);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ5, cjqV.CJQ_JCQ5);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ6, cjqV.CJQ_JCQ6);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ7, cjqV.CJQ_JCQ7);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ8, cjqV.CJQ_JCQ8);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ9, cjqV.CJQ_JCQ9);
                        bll_DataShowModelService.AddOrUpdate(dto.CJQ_Yb, dto.CJQ_JCQ10, cjqV.CJQ_JCQ10);

                        dto.UpdateTIme = DateTime.Now;
                        dto.SesssionID = sessionID.ToString();
                        bllCJQ.Update(dto);

                    }


                }


            }
            else
            {
                // 数据不完整

                return false;
            }



            return true;
        }






        public static CaiJiQiModel Get_CaiJiQiModel(string dataContent)
        {

            CaiJiQiModel dto = new CaiJiQiModel();

            string[] dMsg = dataContent.Split(';');

            dto.CJQ_Addr = dMsg[0].Replace("addr:", "");
            dto.CJQ_SIM = dMsg[1].Replace("sim:", "");
            dto.CJQ_Yb = dMsg[2].Replace("yb:", "");
            dto.CJQ_GPS = dMsg[3].Replace("gps:", "");

            dto.CJQ_Ver = dMsg[4].Replace("ver:", "");
            dto.CJQ_Count = Convert.ToInt32(dMsg[5].Replace("counter:", ""));
            dto.CJQ_JCQ1 = dMsg[6].Replace("reg1:", "");
            dto.CJQ_JCQ2 = dMsg[7].Replace("reg2:", "");
            dto.CJQ_JCQ3 = dMsg[8].Replace("reg3:", "");
            dto.CJQ_JCQ4 = dMsg[9].Replace("reg4:", "");
            dto.CJQ_JCQ5 = dMsg[10].Replace("reg5:", "");
            dto.CJQ_JCQ6 = dMsg[11].Replace("reg6:", "");
            dto.CJQ_JCQ7 = dMsg[12].Replace("reg7:", "");
            dto.CJQ_JCQ8 = dMsg[13].Replace("reg8:", "");
            dto.CJQ_JCQ9 = dMsg[14].Replace("reg9:", "");
            dto.CJQ_JCQ10 = dMsg[15].Replace("reg10:", "");
            dto.UpdateTIme = DateTime.Now;



            return dto;
        }


        public static CaiJiQiModelValue Get_CaiJiQiModelValue(string dataContent)
        {

            CaiJiQiModelValue dto = new CaiJiQiModelValue();

            string[] dMsg = dataContent.Split(';');

            dto.CJQ_Addr = dMsg[0].Replace("addr:", "");

            int dataCount = dMsg.Count();

            for (int i = 1; i < dataCount; i++)
            {
                switch (i)
                {
                    case 1: dto.CJQ_JCQ1 = dMsg[i]; break;
                    case 2: dto.CJQ_JCQ2 = dMsg[i]; break;
                    case 3: dto.CJQ_JCQ3 = dMsg[i]; break;
                    case 4: dto.CJQ_JCQ4 = dMsg[i]; break;
                    case 5: dto.CJQ_JCQ5 = dMsg[i]; break;
                    case 6: dto.CJQ_JCQ6 = dMsg[i]; break;
                    case 7: dto.CJQ_JCQ7 = dMsg[i]; break;
                    case 8: dto.CJQ_JCQ8 = dMsg[i]; break;
                    case 9: dto.CJQ_JCQ9 = dMsg[i]; break;
                    case 10: dto.CJQ_JCQ10 = dMsg[i]; break;
                }
            }

            dto.UpdateTIme = DateTime.Now;



            return dto;
        }



        public static List<string> GetListSession(string addr)
        {
            List<string> outList = new List<string>();

            CaiJiQiModelService bllCJQ = new CaiJiQiModelService();

            outList = bllCJQ.Query(Q => Q.CJQ_Yb == addr).Select(S => S.SesssionID).ToList();
            return outList;
        }


    }


}
